<template>
  <ul class="list">
    <li
      class="item"
      v-for="item of letter"
      :key="item"
      :ref="item"
      @click="getInner"
      @touchstart="touchStart"
      @touchmove="touchMove"
      @touchend="touchEnd"
    >{{item}}</li>
  </ul>
</template>

<script>
export default {
  name: "CityAlphabet",
  props: ["cities"],
  data() {
    return {
      flag: false,
      startY: "",
      timer: null
    };
  },
  updated() {
    this.startY = this.$refs["A"][0].offsetTop;
  },
  methods: {
    getInner(e) {
      //   console.log(e.target.innerHTML);
      this.$emit("send", e.target.innerHTML);
    },
    touchStart() {
      this.flag = true;
    },
    touchMove(e) {
      if (this.flag) {
        if (this.timer) {
          clearTimeout(this.timer);
        }

        //控制节流 提高性能
        this.timer = setTimeout(() => {
          const tTop = e.touches[0].clientY - 89;
          const index = Math.floor(
            (tTop - this.startY) / this.$refs["A"][0].offsetHeight
          );
          if (index >= 0 && index <= this.letter.length - 1) {
            this.$emit("send", this.letter[index]);
          }
        }, 16);
      }
    },
    touchEnd() {}
  },
  computed: {
    letter() {
      const letter = [];
      for (const i in this.cities) {
        letter.push(i);
      }
      return letter;
    }
  }
};
</script>

<style lang="stylus" scoped>
@import '~styles/varibles.styl';

.list {
  width: 0.4rem;
  position: absolute;
  right: 0;
  top: 1.78rem;
  // background red
  bottom: 0;
  display: flex;
  justify-content: center;
  flex-direction: column;

  .item {
    line-height: 0.45rem;
    text-align: center;
    color: $bgColor;
  }
}
</style>
